package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 管理员登录
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //1.将页面提交的密码password进行md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2.根据页面提交的用户名查询数据库
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(wrapper);
        //3.如果查询为空直接返回失败
        if (emp == null) {
            return R.error("用户名或密码错误，登录失败重新试试吧");
        }
        //4.查询密码不正确返回失败
        if (!emp.getPassword().equals(password)) {
            return R.error("用户名或密码错误，登录失败重新试试吧");
        }
        //5.查询员工状态，如果已经禁用状态，则返回失败
        if (emp.getStatus() == 0) {
            return R.error("登录失败哟，账号已经禁用");
        }
        //登录成功，将员工ID存入Session并返回成功
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /***
     * 员工退出
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /***
     * 新增员工
     * @param employee
     * @return
     */
    @PostMapping
    private R<String> seve(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("新增员工代码启动，信息是：{}", employee.toString());
        //设置初始密码MD5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        Long empID = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(empID);
//        employee.setUpdateUser(empID);
        employeeService.save(employee);
        return R.success("新增成功");
    }

    /**
     * 员工信息分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("page = {}，pageSize = {},name = {}", page, pageSize, name);
        //分页构造器

        Page pageInfo = new Page(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper();
        //添加过滤条件
        qw.like(StringUtils.isNotBlank(name), Employee::getName, name);
        //添加排序条件
        qw.orderByDesc(Employee::getUpdateTime);
        //执行查询
        employeeService.page(pageInfo, qw);
        return R.success(pageInfo);
    }
    /**
     * 管理者修改员工信息
     *
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
        log.info(employee.toString());
//        Long empid = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empid);
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    /**
     * 根据ID查询员工信息，回显数据
     *
     * @param
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {
        log.info("id ={}", id);
        Employee byId = employeeService.getById(id);
        if (byId != null) {
            return R.success(byId);
        }
        return R.error("没有查到该员工信息");
    }

}
